<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
</head>

<body onload="initialize();">
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%@include file="connect_database.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String database_name = request.getParameter("database");
String table_name = request.getParameter("table");
String sql_check_table = "show tables like '" + table_name + "';";
ResultSet resultset_check_table = database.executeQuery(sql_check_table, database_name);
if (resultset_check_table != null && resultset_check_table.next() == false)
{
	String name_data = request.getParameter("name_data");
	String type_data = request.getParameter("type_data");
	String size_data = request.getParameter("size_data");
	String precision_data = request.getParameter("precision_data");
	String unsigned_data = request.getParameter("unsigned_data");
	String zerofill_data = request.getParameter("zerofill_data");
	String binary_data = request.getParameter("binary_data");
	String enumeration_data = request.getParameter("enumeration_data");
	String notnull_data = request.getParameter("notnull_data");
	String default_data = request.getParameter("default_data");
	String autoincremental_data = request.getParameter("autoincremental_data");
	String unique_data = request.getParameter("unique_data");
	String primarykey_data = request.getParameter("primarykey_data");
	String foreigntable_data = request.getParameter("foreigntable_data");
	String foreignfield_data = request.getParameter("foreignfield_data");
	String index_data = request.getParameter("index_data");
	String indexorder_data = request.getParameter("indexorder_data");
	String message = "";
	String [] insert_name = name_data.split(",");
	String [] insert_type = type_data.split(",");
	String [] insert_size = size_data.split(",");
	String [] insert_precision = precision_data.split(",");
	String [] insert_unsigned = unsigned_data.split(",");
	String [] insert_zerofill = zerofill_data.split(",");
	String [] insert_binary = binary_data.split(",");
	String [] insert_enumeration = enumeration_data.split(";");
	String [] insert_notnull = notnull_data.split(",");
	String [] insert_default = default_data.split(",");
	String [] insert_autoincremental = autoincremental_data.split(",");
	String [] insert_unique = unique_data.split(",");
	String [] insert_primarykey = primarykey_data.split(",");
	String [] insert_foreigntable = foreigntable_data.split(",");
	String [] insert_foreignfield = foreignfield_data.split(",");
	String [] insert_index = index_data.split(",");
	String [] insert_indexorder = indexorder_data.split(",");
	String fields = "";
	for (int i = 0; i < insert_name.length; i++)
	{
		insert_name[i] = insert_name[i].trim();
		insert_type[i] = insert_type[i].trim();
		insert_size[i] = insert_size[i].trim();
		insert_precision[i] = insert_precision[i].trim();
		insert_unsigned[i] = insert_unsigned[i].trim();
		insert_zerofill[i] = insert_zerofill[i].trim();
		insert_binary[i] = insert_binary[i].trim();
		insert_enumeration[i] = insert_enumeration[i].trim();
		insert_notnull[i] = insert_notnull[i].trim();
		insert_default[i] = insert_default[i].trim();
		insert_autoincremental[i] = insert_autoincremental[i].trim();
		insert_unique[i] = insert_unique[i].trim();
		insert_primarykey[i] = insert_primarykey[i].trim();
		insert_foreigntable[i] = insert_foreigntable[i].trim();
		insert_foreignfield[i] = insert_foreignfield[i].trim();
		insert_index[i] = insert_index[i].trim();
		insert_indexorder[i] = insert_indexorder[i].trim();
	}
	for (int i = 0; i < insert_name.length; i++)
	{
		fields = fields + insert_name[i] + " " + insert_type[i];
		if (insert_size[i].length() > 0 || insert_precision[i].length() > 0)
			fields = fields + "(" + (insert_size[i].length() > 0 ? insert_size[i] : "") + (insert_precision[i].length() > 0 ? "," + insert_precision[i] : "") + ")";
		if (insert_unsigned[i].equals("true") == true)
			fields = fields + " unsigned";
		if (insert_zerofill[i].equals("true") == true)
			fields = fields + " zerofill";
		if (insert_binary[i].equals("true") == true)
			fields = fields + " binary";
		if (insert_enumeration[i].length() > 0)
			fields = fields + "('" + insert_enumeration[i].replaceAll(",", "','") + "')";
		if (insert_notnull[i].equals("true") == true)
			fields = fields + " not null";
		if (insert_default[i].length() > 0)
		{
			if (insert_default[i].toUpperCase().equals("CURRENT_TIMESTAMP") == true)
				fields = fields + " default " + insert_default[i];
			else
				fields = fields + " default '" + insert_default[i] + "'";
		}
		if (insert_autoincremental[i].equals("true") == true)
			fields = fields + " auto_increment";
		if (insert_unique[i].equals("true") == true)
			fields = fields + " unique key";
		if (insert_primarykey[i].equals("true") == true)
			fields = fields + " primary key";
		fields = fields + ",";
	}
	for (int i = 0; i < insert_name.length; i++)
	{
		if (insert_foreigntable[i].equals("　　　　") == false && insert_foreignfield[i].equals("　　　　") == false)
		{
			fields = fields + "foreign key (" + insert_name[i] + ") references " + insert_foreigntable[i] + " (" + insert_foreignfield[i] + "),";
		}
	}
	if (index_data.indexOf("true") > -1)
	{
		fields = fields + "index (";
		for (int i = 1; i <= insert_name.length; i++)
		{
			if (indexorder_data.indexOf(String.valueOf(i)) == -1)
				break;
			for (int j = 0; j < insert_name.length; j++)
			{
				if (insert_index[j].equals("true") == true && insert_indexorder[j].equals(String.valueOf(i)) == true)
				{
					fields = fields + insert_name[j] + ",";
					break;
				}
			}
		}
		fields = fields.substring(0, fields.length() - 1) + ")";
	}
	if (fields.charAt(fields.length() - 1) == ',')
		fields = fields.substring(0, fields.length() - 1);
	String sql_insert = "create table " + table_name + " (" + fields + ");";
	boolean success = database.executeUpdate(sql_insert, database_name);
	if (success == true)
	{
	%>
	<script type="text/javascript">
	<!--
		window.alert("数据表 <%= table_name %> 添加成功！");
		window.location.href = "tables.jsp?database=<%= database_name %>";
	//-->
	</script>
	<%
	}
	else
	{
	%>
	<script type="text/javascript">
	<!--
		window.alert("数据表 <%= table_name %> 添加失败，请重新设置各字段信息！");
		window.history.go(-1);
	//-->
	</script>
	<%
	}
}
else if (resultset_check_table == null)
{
%>
<script type="text/javascript">
<!--
	window.alert("连接数据库 <%= database_name %> 失败！");
	window.location.href = "databases.jsp";
//-->
</script>
<%
}
else
{
%>
<script type="text/javascript">
<!--
	window.alert("数据表 <%= table_name %> 已存在！");
	window.history.go(-3);
//-->
</script>
<%
}
if (resultset_check_table != null)
	resultset_check_table.close();
database.closeConnection();
%>
</body>
</html>